import { useTableActionRedux } from "@/components/ant-ext-table/redux/HAdminContext";
import { useActionForm, useEdit } from "@/common/hooks";
import HModal from "@/components/ant-ext/HModal";
import { Form } from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import { AdminType } from "@/const/const";
import SysAnnounce, { SysAnnounceEntity } from "@/api/sys-announce";
import { useState } from "react";

export default function AdminEdit() {
    const [loading, setLoading] = useState(false)
    const { action, reset } = useTableActionRedux<SysAnnounceEntity>()
    const { form, key, title } = useActionForm(action, [SysAnnounce.EDIT], SysAnnounce.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    const saveAndFlush = useEdit(key === SysAnnounce.EDIT ? SysAnnounce.doEdit : SysAnnounce.doSave, {
        setLoading: setLoading,
        success: () => {
            reset()
        }
    })

    return <>
        <HModal onCancel={close} type={"setUp"} width={550} onOk={form.submit}
            confirmLoading={loading}
            visible={title !== undefined}
            title={title}
        >
            <Form name={"editForm"} form={form} labelCol={{ span: 4 }}
                onFinish={() => {
                    saveAndFlush({ body: { ...form.getFieldsValue() } })
                }}>
                <HFormItem label={"id"} name={"id"} hidden={key === SysAnnounce.ADD}
                    disabled={key !== SysAnnounce.ADD} />
                <HFormItem label={"部门id"} name={"deptId"} hidden={key === SysAnnounce.ADD}
                    disabled={key !== SysAnnounce.ADD} />
                <HFormItem label={"内容"} name={"content"} type={"textarea"} rows={6} required />
                <HFormItem label={"用户类型"} name={"types"} type={"checkbox"} dataSource={AdminType.select} required />
            </Form>
        </HModal>
    </>
}